package com.example.flowermall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.flowermall.entity.InvitationOrder;
import com.example.flowermall.entity.Product;
import com.example.flowermall.entity.Result;
import com.example.flowermall.service.InvitationOrderService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/invitationOrder")
public class InvitationOrderController {
    private final InvitationOrderService invitationOrderService;

    public InvitationOrderController(InvitationOrderService invitationOrderService) {
        this.invitationOrderService = invitationOrderService;
    }


    @GetMapping("/list")
    public Result getList(@RequestParam(value = "pageNo",required = false)Long pageNo,
                          @RequestParam(value = "pageSize",required = false)Long pageSize,
                          @RequestParam(value = "userId",required = false)Integer userId){
        Page<InvitationOrder> page=new Page<>(pageNo,pageSize);
        Map<String,Object> data=new HashMap<>();
        Result result=Result.success();
        LambdaQueryWrapper<InvitationOrder> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null,InvitationOrder::getUserId,userId);
        Page<InvitationOrder> page1 = invitationOrderService.page(page, queryWrapper);
        List<InvitationOrder> records = page1.getRecords();
        data.put("total",page.getTotal());
        data.put("rows",records);
        result.put("data",data);
        return result;
    }

    @PostMapping("/insert")
    public Result insertGoods(@RequestBody InvitationOrder invitationOrder){
        Integer insert = invitationOrderService.insert(invitationOrder);
        if(insert > 0){
            return Result.success("提交成功");
        }
        return Result.error("提交失败");
    }
}
